log using "E:\FIS_duration\max_onloan_avail_util_rep.log"
infix episode 2-5 str ticker 7-12 type 14-14 ndays 16-19 numol 21-24 olbeg 26-34 olmax 36-44 olend 46-54 retob 56-64 retoa 66-74 numav 76-79 avbeg 81-89 avmax 91-99 avend 101-109 retavb 111-119 retava 121-129 numu 130-133 ubeg 135-143 umax 145-153 uend 155-163 retub 165-173 retua 175-183 using "e:\FIS\episode_rets_chg_onloan_avail_rep.txt"
summarize retob if retob >= -1
summarize retoa if retoa >= -1
summarize retob if retob >= -1 & type == 1
summarize retob if retob >= -1 & type == 2
summarize retob if retob >= -1 & type == 3
summarize retoa if retoa >= -1 & type == 1
summarize retoa if retoa >= -1 & type == 2
summarize retoa if retoa >= -1 & type == 3
summarize retavb if retavb >= -1
summarize retava if retava >= -1
summarize retavb if retavb >= -1 & type == 1
summarize retavb if retavb >= -1 & type == 2
summarize retavb if retavb >= -1 & type == 3
summarize retava if retava >= -1 & type == 1
summarize retava if retava >= -1 & type == 2
summarize retava if retava >= -1 & type == 3
summarize retub if retub >= -1
summarize retua if retua >= -1
summarize retub if retub >= -1 & type == 1
summarize retub if retub >= -1 & type == 2
summarize retub if retub >= -1 & type == 3
summarize retua if retua >= -1 & type == 1
summarize retua if retua >= -1 & type == 2
summarize retua if retua >= -1 & type == 3

summarize olbeg olmax olend if olbeg >= 0 & olmax >= 0 & olend == 0
summarize olbeg olmax olend if olbeg >= 0 & olmax >= 0 & olend >= 0
summarize avbeg avmax avend if avbeg >= 0 & avmax >= 0 & avend >= 0
summarize ubeg umax uend if ubeg >= 0 & umax >= 0 & uend >= 0
gen lastdo = numol == ndays
gen lastda = numav == ndays
gen lastdu = numo == ndays
gen firstdo = numol == 1
gen firstda = numav == 1
gen firstdu = numu == 1
summarize lastdo lastda lastdu firstdo firstda firstdu
gen pctdayo = numo/ndays
gen pctdaya = numa/ndays
gen pctdayu = numu/ndays
summarize lastdo lastda firstdo firstda pctdayo pctdaya pctdayu
summarize lastdo firstdo pctdayo if numo > 0
summarize lastda firstda pctdaya if numa > 0
summarize lastdu firstdu pctdayu if numa > 0
gen difreto = retob - retoa
gen difreta = retavb - retava
summarize difreto if retob > -1 & retoa > -1
summarize difreta if retavb > -1 & retava > -1
summarize difreto if retob > -1 & retoa > -1 & type == 1
summarize difreto if retob > -1 & retoa > -1 & type == 2
summarize difreto if retob > -1 & retoa > -1 & type == 3
summarize difreta if retavb > -1 & retava > -1 & type == 1
summarize difreta if retavb > -1 & retava > -1 & type == 2
summarize difreta if retavb > -1 & retava > -1 & type == 3
log close